# coding:utf-8

import time
import thread

gen = None

# def long_io():
#     print("开始调用long_Io")
#     def fun():
#         print("start fun内部真实处理")
#         time.sleep(2)
#         print("end fun内部真实处理")
#         global gen
#         try:
#             gen.send(1)
#         except StopIteration:
#             pass
#     thread.start_new_thread(fun, ())
#     print("long_Io函数返回")

def long_io():
    print("开始调用long_Io")
    time.sleep(2)
    print("long_Io函数返回")
    yield 1


def gen_coroutine(f):
    def wrapper(*args, **kwargs):
        gen_req_a = f() # => gen_req_a = req_a()
        gen_long_io = gen_req_a.next() # => gen_long_io = long_io()
        def fun():
            ret = gen_long_io.next()
            try:
                gen_req_a.send(ret)
            except StopIteration:
                pass
        thread.start_new_thread(fun, ())
    return wrapper

# @gen_coroutine
# def req_a()
# req_a = gen_coroutine(req_a)
@gen_coroutine
def req_a():
    print("start calling a")
    ret = yield long_io()
    print("ret %s" % ret)
    print("end a")


def req_b():
    print("start b")
    time.sleep(1)
    print("end b")


def main():
    """模拟主循环调度者"""
    # global  gen
    # gen = req_a()
    # gen.next()
    req_a()
    req_b()
    while 1:
        pass

if __name__ == "__main__":
    main()